package com.ymatou.iapi.trading.sellerquery.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery.parameter.GetOrderCreditListBean;
import com.ymatou.iapi.trading.sellerquery.parameter.resp.GetOrderCreditListResp;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.common.entitie.OrderCreditInfo;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取维权订单列表 FreeMaker Template自动生成代码
 * 接口已转接java
 */
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetOrderCreditList {

	private static GetOrderCreditListBean goclbean;
	private static com.ymatou.iapi.trading.sellerquery.service.GetOrderCreditListCall goclcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取维权订单列表");
	}

	@Before
	public void caseUp() {

		goclbean = new GetOrderCreditListBean();
		goclcall = new com.ymatou.iapi.trading.sellerquery.service.GetOrderCreditListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(goclcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verify(){
		GetOrderCreditListResp expectResponse = new GetOrderCreditListResp();
		ConstructExpectedResult(expectResponse);
		GetOrderCreditListResp actualResponse =  goclcall.getOrderCreditListResp();
		List<OrderCreditInfo> expects = expectResponse.getCreditInfoList();
		List<OrderCreditInfo> actuals = actualResponse.getCreditInfoList();
		if(expects==null){
			DataUtil.verify(null, actuals, "验证creditInfoList==null");
		}else{
			for(int i=0;i<expects.size();i++){
				OrderCreditInfo expect = expects.get(i);
				OrderCreditInfo actual = actuals.get(i);
				DataUtil.verify(expect.getCreditDetailId(), actual.getCreditDetailId(),"验证CreditDetailId");
				DataUtil.verify(expect.getUserId(), actual.getUserId(),"验证UserId");
				DataUtil.verify(expect.getTargetId(), actual.getTargetId(),"验证TargetId");
				DataUtil.verify(expect.getServiceRange(), actual.getServiceRange(),"验证ServiceRange");
				DataUtil.verify(expect.getDescription(), actual.getDescription(),"验证Description");
				DataUtil.verify(expect.getSort(), actual.getSort(),"验证Sort");
				DataUtil.verify(expect.getIp(), actual.getIp(),"验证ip");
				DataUtil.verify(expect.getType(), actual.getType(),"验证Type");
				DataUtil.verify(expect.getAddTime(), actual.getAddTime(),"验证AddTime");
				DataUtil.verify(expect.getUpdateTime(), actual.getUpdateTime(),"验证UpdateTime");
				DataUtil.verify(expect.getAction(), actual.getAction(),"验证Action");
				DataUtil.verify(expect.getTargetUserId(), actual.getTargetUserId(),"验证TargetUserId");
				DataUtil.verify(expect.getSimilarRange(), actual.getSimilarRange(),"验证SimilarRange");
				DataUtil.verify(expect.getPraiseRange(), actual.getPraiseRange(),"验证PraiseRange");
				DataUtil.verify(expect.getSellerReply(), actual.getSellerReply(),"验证SellerReply");
				DataUtil.verify(expect.getSellerReplyUpdateTime(), actual.getSellerReplyUpdateTime(),"验证SellerReplyUpdateTime");
				DataUtil.verify(expect.getFromPurchaseAgent(), actual.getFromPurchaseAgent(),"验证FromPurchaseAgent");
			}
		}

	}
	
	private void ConstructExpectedResult(GetOrderCreditListResp response){
		List<Map> list = execute(goclbean.getOrderId());
		response.setCreditInfoList(convert(list));
	}
	
	private List<Map> execute(int targetId){
		DefaultWapper db = new DefaultWapper();
		List<Map> list = db.executeSql(getOrderCreditList(targetId));
		return list;
	}
	
	private List<OrderCreditInfo> convert(List<Map> list){
		List<OrderCreditInfo> creditInfoList = null;
		if(false == DataUtil.isNullOrEmpty(list)){
			creditInfoList = new ArrayList<OrderCreditInfo>();
			for(Map m:list){
				OrderCreditInfo o = new OrderCreditInfo();
				DataUtil.mapToBean(o, m);
				creditInfoList.add(o);
			}
		}
		return creditInfoList;
	}
	
	public static String getOrderCreditList(int targetId) {
		return "SELECT iAction as action,"
				+ "dAddTime as addTime,"
				+ "iCreditDetailId as creditDetailId"
				+ ",sDescription as description,"
				+ "bFromPurchaseAgent as fromPurchaseAgent,"
				+ "sIP as ip,"
				+ "iPoint3 as praiseRange,"
				+ "sSellerReply as sellerReply,"
				+ "dSellerReplyUpdateTime as sellerReplyUpdateTime,"
				+ "fPoint as serviceRange,"
				+ "isnull(fPoint2,0) as similarRange,"
				+ "iSort as sort,"
				+ "  sTargetId as targetId,"
				+ "isnull(iTargetUserId,0) as targetUserId,"
				+ "iType as type,"
				+ "dUpdateTime as updateTime,"
				+ "iUserId as userId"
				+ " FROM [Ymt_CreditDetail](NOLOCK) where sTargetId ="
				+ targetId;
	}
	
	/**
	 * 随机从Ymt_CreditDetail表中获取一个sTargetId
	 * @return
	 */
	public static int getRandomTargetId(){
		int targetId = 0;
		List<Map> list = new DefaultWapper().executeSql("select top 10 sTargetId from Ymt_CreditDetail order by sTargetId desc");
		if(false == DataUtil.isNullOrEmpty(list)){
			int num = DataUtil.Random.getRandomIntFromRange(1, list.size());
			targetId = Integer.parseInt(String.valueOf(list.get(num).get("sTargetId")));
		}
		return targetId;
	}


	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCreditList_001() {
		Logger.start(true,"获取维权订单列表成功");
		try {
			int OrderId = getRandomTargetId();
			goclbean.setOrderId(OrderId);
			String s=FormatUtil.beanToHttpString(goclbean, true, false);
			goclcall.setData(s);
			goclcall.callService();
			Logger.verifyEquals(ECode.SUCCESS, goclcall.getCode(), "验证Code");
			verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCreditList_002() {
		Logger.start(true,"订单号为空，获取维权订单列表失败");
		try {
			String s=FormatUtil.beanToHttpString(goclbean, true, false);
			goclcall.setData(s);
			goclcall.callService();
			Logger.verifyEquals(ECode.FAILED400, goclcall.getCode(), "验证code");
			Logger.verifyEquals(ECode.BCODE8, goclcall.getBCode(), "验证bcode");
			Logger.verifyEquals("orderId:订单号未提供", goclcall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderCreditList_003() {
		Logger.start(true,"订单号不存在，获取维权订单列表为空,成功");
		try {
			goclbean.setOrderId(404);
			String s=FormatUtil.beanToHttpString(goclbean, true, false);
			goclcall.setData(s);
			goclcall.callService();
			Logger.verifyEquals(ECode.SUCCESS, goclcall.getCode(), "验证code");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetOrderCreditList_004() {
		Logger.start(true,"获取维权订单列表成功");
		try {
			int OrderId = getRandomTargetId();
			goclbean.setOrderId(OrderId);
			String s=FormatUtil.beanToHttpString(goclbean, true, false);
			goclcall.setData(s);
			goclcall.callService();
			Logger.verifyEquals(ECode.SUCCESS, goclcall.getCode(), "验证Code");
			verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetOrderCreditList_005() {
		Logger.start(true,"获取维权订单列表成功");
		try {
			int OrderId = getRandomTargetId();
			goclbean.setOrderId(OrderId);
			String s=FormatUtil.beanToHttpString(goclbean, true, false);
			goclcall.setData(s);
			goclcall.callService();
			Logger.verifyEquals(ECode.SUCCESS, goclcall.getCode(), "验证Code");
			verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}